import { queryProjectList, queryProjectInfo, updateProject, addProject, deleteProject, queryCurrentUserOwnedProjects } from '../services/project';
import _ from 'lodash';

export default {
  namespace: 'project',

  state: {
    list: [],
    currentProject: {},
    formModalVisible: false,
    status: 1, //1：新建， 2：编辑， 3：查看
    currentUserOwnedProjectList: []
  },

  effects: {
    *list({ payload }, { call, put }) {
      const response = yield call(queryProjectList, payload);
      let plist = [];
      if (response.code == 0) {
        plist = response.data;
      }
      yield put({
        type: 'queryList',
        payload: Array.isArray(plist) || (!plist.length === 0) ? plist : [],
      });
    },
    *create({ payload }, { call, put }) {
      let project = {
        name: payload.name,
        description: payload.description,
        users: _.union(payload.developer, payload.pm, payload.tester),
        ding:payload.ding
      }
      const response = yield call(addProject, project);
      if (response.code === 0) {
        yield put({
          type: 'appendList',
          payload: {
            id: response.data.projectId,
            name: payload.name,
            description: payload.description,
            ding:payload.ding
          }
        })
        yield put({
          type: 'setFormModelVisible',
          payload: false
        });
      }
    },
    *edit({ payload }, { call, put }) {
      let project = {
        id: payload.id,
        name: payload.name,
        description: payload.description,
        users: _.union(payload.developer, payload.pm, payload.tester),
        ding: payload.ding
      }
      const response = yield call(updateProject, project);
      if (response.code === 0) {
        let listresponse = yield call(queryProjectList);
        let newlist = listresponse.data;
        yield put({
          type: 'queryList',
          payload: newlist
        });
        yield put({
          type: 'setFormModelVisible',
          payload: false
        });
      }
    },
    *delete({ payload }, { call, put }) {
      let projectId = payload;
      let response = yield call(deleteProject, projectId);
      if (response.code === 0) {
        yield put({
          type: 'removeFromList',
          payload: projectId
        })
      }
    },
    *query({payload}, { call, put }){
      let response = yield call(queryProjectInfo, payload);
      yield put({
        type: 'setCurrentProject',
        payload: response.code === 0 ? { ...response.data }:{}
      });
    },
    *setFormModalVisible({ payload }, { call, put }) {
      yield put({
        type: 'setFormModelVisible',
        payload: payload
      });
    },
    *showCreateFormModal({ payload }, { call, put }) {
      yield put({
        type: 'setStatus',
        payload: 1
      });
      yield put({
        type: 'setFormModelVisible',
        payload: true
      });

    },
    *showEditFormModal({ payload }, { call, put }) {
      let response = yield call(queryProjectInfo, payload);
      if (response.code === 0) {
        // let project = response.data.project;
        // let tester = response.data.tester;
        // let pm = response.data.pm;
        // let developer = response.data.developer;
        yield put({
          type: 'setCurrentProject',
          payload: { ...response.data }
        });
        yield put({
          type: 'setStatus',
          payload: 2
        });
        yield put({
          type: 'setFormModelVisible',
          payload: true
        });
      }
    },
    *showCheckFormModal({ payload }, { call, put }) {
      let response = yield call(queryProjectInfo, payload);
      if (response.code === 0) {
        yield put({
          type: 'setCurrentProject',
          payload: { ...response.data }
        });
        yield put({
          type: 'setStatus',
          payload: 3
        });
        yield put({
          type: 'setFormModelVisible',
          payload: true
        });
      }
    },
    *queryCurrentUserOwnedProjects({ payload }, { call, put, select }) {
      let response = yield call(queryCurrentUserOwnedProjects, payload);
      if (response.code === 0) {
        yield put({
          type: 'setCurrentUserOwnedProjectList',
          payload: response.data.projects
        })
        yield put({
          type: 'version/setList',
          payload: response.data.versions
        })
        if(payload && payload.versionId){
          yield put({
            type:'version/setCurrentVersion',
            payload: {id: payload.versionId, name: payload.versionName}
          })
        }else{
          yield put({
            type:'version/setCurrentVersion',
            payload: response.data.versions[0]
          })
        }
        
        yield put({
          type: 'setCurrentProject',
          payload:  response.data.currentProject
        })
      }
    }
  },

  reducers: {
    queryList(state, action) {
      return {
        ...state,
        list: action.payload,
      };
    },
    appendList(state, action) {
      return {
        ...state,
        list: state.list.concat(action.payload),
      };
    },
    removeFromList(state, action) {
      let list = _.remove(state.list, (project) => {
        return project.id !== action.payload
      })
      return {
        ...state,
        list: list
      };
    },
    setCurrentProject(state, action) {
      return {
        ...state,
        currentProject: action.payload
      }
    },
    setFormModelVisible(state, action) {
      return {
        ...state,
        formModalVisible: action.payload
      }
    },
    setStatus(state, action) {
      return {
        ...state,
        status: action.payload
      }
    },
    setCurrentUserOwnedProjectList(state, action) {
      return {
        ...state,
        currentUserOwnedProjectList: action.payload
      }
    }

  },
};
